
package com.afd.ozblip.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.afd.ozblip.activity.BaseActivity.ListDialogListener;
import com.afd.ozblip.adapter.BusinessListAdapter;
import com.afd.ozblip.app.CommonInfo;
import com.afd.ozblip.entity.BusinessEntity;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TopFragment extends Fragment {

    /****************** 视图变量 **************************/
    private RadioGroup conditonRg;
    private ListView topListView;
    private LinearLayout changeCategoryLayout;
    private TextView categoryTv;
    /****************** 其它变量 **************************/
    private IndexActivity mActivity;
    private List<BusinessEntity> businessTopList;
    private BusinessListAdapter businessListAdapter;
    private List<HashMap<String, Object>> categoryList = null;
    private String parentClassName = "美食";
    private int sortVal = 6;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mActivity = (IndexActivity) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException("Activity 必须是 "
                    + IndexActivity.class.getName());
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 获取参数
        getBundle();
    }

    @Override
    public void onResume() {
        super.onResume();
        // 获取列表数据
        requestTopListData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View rootView = inflater.inflate(
                R.layout.top_list, container, false);
        // 寻找视图
        findViews(rootView);
        // 设置监听
        setListeners();
        return rootView;
    }

    /**
     * 寻找视图
     */
    public void findViews(View rootView) {
        this.topListView = (ListView) rootView.findViewById(R.id.lv_top_businessList);
        this.conditonRg = (RadioGroup) rootView.findViewById(R.id.rg_top_condition);
        this.changeCategoryLayout = (LinearLayout) rootView
                .findViewById(R.id.ll_top_change_category);
        this.categoryTv = (TextView) rootView.findViewById(R.id.tv_top_category);
        categoryList = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> smap1 = new HashMap<String, Object>();
        smap1.put("key", "1");
        smap1.put("value", "美食");
        categoryList.add(smap1);
        HashMap<String, Object> smap2 = new HashMap<String, Object>();
        smap2.put("key", "2");
        smap2.put("value", "休闲娱乐");
        categoryList.add(smap2);
        HashMap<String, Object> smap3 = new HashMap<String, Object>();
        smap3.put("key", "3");
        smap3.put("value", "生活服务");
        categoryList.add(smap3);
        HashMap<String, Object> smap4 = new HashMap<String, Object>();
        smap4.put("key", "4");
        smap4.put("value", "旅馆酒店");
        categoryList.add(smap4);
        HashMap<String, Object> smap5 = new HashMap<String, Object>();
        smap5.put("key", "5");
        smap5.put("value", "购物");
        categoryList.add(smap5);
        HashMap<String, Object> smap6 = new HashMap<String, Object>();
        smap6.put("key", "6");
        smap6.put("value", "运动健身");
        categoryList.add(smap6);
        HashMap<String, Object> smap7 = new HashMap<String, Object>();
        smap7.put("key", "7");
        smap7.put("value", "爱车");
        categoryList.add(smap7);
    }

    /**
     * 设置监听
     */
    public void setListeners() {

        View.OnClickListener topClick = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.ll_more_layout_setting:
                        Intent settingIntent = new Intent(mActivity,
                                SettingActivity.class);
                        startActivity(settingIntent);
                        break;
                    case R.id.ll_top_change_category:
                        showListDialog(categoryList, new ListDialogListener() {

                            @Override
                            public void doSomething(int position) {
                                categoryTv.setText(categoryList.get(position)
                                        .get("value").toString());
                                // categoryTv.setTag(categoryList.get(position).get(
                                // "key"));
                                parentClassName = categoryList.get(position)
                                        .get("value").toString();
                                // 获取列表数据
                                businessListAdapter.removeAllItem();
                                businessListAdapter.notifyDataSetChanged();
                                requestTopListData();
                            }
                        });
                        break;

                    default:
                        break;
                }
            }
        };
        this.changeCategoryLayout.setOnClickListener(topClick);
        this.topListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> itemView, View arg1,
                    int parameInt, long arg3) {
                Intent businessIntent = new Intent(mActivity,
                        BusinessInfoActivity.class);
                BusinessEntity business = businessTopList.get(parameInt);
                businessIntent.putExtra("businessInfo", business);
                startActivity(businessIntent);
            }
        });
        this.conditonRg
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        switch (checkedId) {
                            case R.id.rb_top_hot:
                                sortVal = 6;
                                break;
                            case R.id.rb_top_best:
                                sortVal = 7;
                                break;
                            case R.id.rb_top_new:
                                sortVal = 8;
                                break;

                            default:
                                break;
                        }
                        // 获取列表数据
                        requestTopListData();
                    }
                });
    }

    private Dialog dialog;

    public void showListDialog(final List<HashMap<String, Object>> lists,
            final ListDialogListener dialogListener) {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.dialog_list,
                null);
        dialog = new Dialog(mActivity, R.style.listDialog);
        ListView userlist = (ListView) view.findViewById(R.id.lv_dialog_list);
        SimpleAdapter adapter = new SimpleAdapter(mActivity, lists,
                R.layout.dialog_list_item, new String[] {
                        "value"
                },
                new int[] {
                        R.id.tv_dialog_list_item_name
                });
        userlist.setAdapter(adapter);
        userlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                dialogListener.doSomething(position);
                dialog.dismiss();
            }

        }); // 当每一条目被点击后出发的事件
        dialog.setContentView(view); // 设置显示用户信息列表的对话框视图
        dialog.show();
    }

    /**
     * 获取传递的参数
     */
    private void getBundle() {
        // Intent intent = getIntent();
    }

    /**
     * TODO 获取排行榜列表数据
     */
    private void requestTopListData() {

        AsyncLoadData ald = new AsyncLoadData(mActivity, "加载中...",
                new AsyncAction() {

                    @Override
                    public void doLoad() throws Exception {
                        // 获取提交参数
                        Map<String, Object> nearbyBusinessParamMap = new HashMap<String, Object>();
                        nearbyBusinessParamMap.put("parClass", parentClassName);
                        nearbyBusinessParamMap.put("sellerScope", "1000000000");
                        if (CommonInfo.commonLocation != null) {
                            nearbyBusinessParamMap.put("userLongitude",
                                    CommonInfo.commonLocation.getLatitude());
                            nearbyBusinessParamMap.put("userDimension",
                                    CommonInfo.commonLocation.getLongitude());
                        }
                        nearbyBusinessParamMap.put("pageCnt", "1");
                        nearbyBusinessParamMap.put("pageRow", "20");
                        nearbyBusinessParamMap.put("sort", sortVal);
                        nearbyBusinessParamMap.put("language", CommonInfo.commonLanguage);
                        // 发送请求
                        byte[] commentItemList = HttpUtil.requestByPost(
                                mActivity, Constants.SELLER_LIST_API,
                                nearbyBusinessParamMap);
                        if (commentItemList != null) {
                            Map<String, Object> resultMap = new ParseJson()
                                    .parseBusinessListJson(commentItemList);
                            businessTopList = (List<BusinessEntity>) resultMap
                                    .get("data");
                        }
                    }

                    @Override
                    public void loadEnd() throws Exception {
                        setTopListAdapter();
                    }

                });
        ald.execute(0);
    }

    /**
     * 调用评论列表适配器
     */
    public void setTopListAdapter() {
        if (CollectionUtil.UnNullAndZero(businessTopList)) {
            this.businessListAdapter = new BusinessListAdapter(mActivity);
            this.businessListAdapter.addItems(businessTopList);
            this.topListView.setAdapter(this.businessListAdapter);

        }
    }
}
